﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Threading;
using FaithCMSControls;

namespace FaithCMS
{
    //TODO: Implement session logout and username display

    public partial class AdminLoginPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Used for disabling page caching (prevents user from returning to login page)
            HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
            HttpContext.Current.Response.Cache.SetValidUntilExpires(false);
            HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            HttpContext.Current.Response.Cache.SetNoStore(); 

            //Register for the TargetedLogin Control's UserAuthenticated event
            TargetedLogin1.UserAuthenticated += new FaithCMSControls.TargetedLogin.AuthenticatedHandler(TargetedLogin1_UserAuthenticated);

            //If the browser supports cookies
            if (Request.Browser.Cookies)
            {
                //If the user opted to remember their password
                if (Request.Cookies["FAITHCMS_LOGIN"] != null)
                {
                    //Retrieve the values and fill in the textboxes with them
                    TargetedLogin.UserText = Request.Cookies["FAITHCMS_LOGIN"].Values["Username"];
                    TargetedLogin.PasswordText = Request.Cookies["FAITHCMS_LOGIN"].Values["Password"];

                    //Set the user id in the SessionManager
                    SessionManager.UserID = Request.Cookies["FAITHCMS_LOGIN"].Values["Username"];

                    //Redirect to the admin dashboard after displaying a message for a few seconds
                    TargetedLogin1.Controls.Add(new LiteralControl("<p class=\"userLoggedInMessage\">You are already logged in! Redirecting to Dashboard in 4 seconds....</p>"));

                    HtmlMeta equiv = new HtmlMeta();
                    equiv.HttpEquiv = "refresh";
                    equiv.Content = "4; url=/Admin/AdminPage.aspx";
                    Header.Controls.AddAt(0, equiv);
                }
                else
                {
                    //Set the SessionManager user status to "LoggedOut"
                    SessionManager.UserStatus = SessionManager.Status.LoggedOut;
                }
            }
        }

        /// <summary>
        /// UserAuthenticated EventHandler
        /// </summary>
        /// <param name="o">Object that triggered the event</param>
        /// <param name="e">Event arguments</param>
        void TargetedLogin1_UserAuthenticated(object o, EventArgs e)
        {
            //If the destination is not null and does not equal "No Destination"
            if (FaithCMSControls.TargetedLogin.Destination != null && FaithCMSControls.TargetedLogin.Destination != "No Destination")
            {
                //Set the SessionManager user status to "LoggedIn"
                SessionManager.UserStatus = SessionManager.Status.LoggedIn;

                //Redirect to the desired destination
                Response.Redirect(FaithCMSControls.TargetedLogin.Destination);
            }
        }
    }
}